package com.xbongbong.pro.rabbitmq.consumer;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.pojo.dto.DistributorMessageRabbitMqDTO;
import com.xbongbong.pro.message.pojo.dto.MessageRabbitMqDTO;
import com.xbongbong.pro.message.pojo.vo.MessageRabbitMqVO;
import com.xbongbong.pro.rabbitmq.binders.OrderCenterConsumerBinder;
import com.xbongbong.pro.rabbitmq.constant.OrderCenterBinderConstant;
import com.xbongbong.pro.rabbitmq.feign.CrmFeignClient;
import com.xbongbong.pro.rabbitmq.feign.OrderCenterFeignClient;
import feign.RetryableException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.handler.annotation.Header;

import javax.annotation.Resource;

@EnableBinding(OrderCenterConsumerBinder.class)
public class OrderCenterListener {

    private static final Logger logger = LoggerFactory.getLogger(OrderCenterListener.class);

    @Resource
    private OrderCenterFeignClient orderCenterFeignClient;

    @StreamListener(value = OrderCenterBinderConstant.ORDER_INPUT)
    public void orderReceive(DistributorMessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case DISTRIBUTOR_ORDER_ADD_PUSH:
                case ORDER_ADD_PUSH:
                    response = orderCenterFeignClient.ordertAdd(message);
                    break;
                case ORDER_CHANGE_PUSH:
                    response = orderCenterFeignClient.orderChange(message);
                    break;
                case ORDER_TEAM_ADD_DEL_MAIN_PUSH:
                    response = orderCenterFeignClient.orderTeamMain(message);
                    break;
                case ORDER_TEAM_ADD_DEL_SYNERGETIC_PUSH:
                    response = orderCenterFeignClient.orderTeamSynergetic(message);
                    break;
                case DISTRIBUTOR_ORDER_REVIEW_PUSH:
                    response = orderCenterFeignClient.orderReview(message);
                    break;
                case DISTRIBUTOR_ORDER_ABANDONED_PUSH:
                    response = orderCenterFeignClient.orderAbandoned(message);
                    break;
                case DISTRIBUTOR_ORDER_CLOSE_PUSH:
                    response = orderCenterFeignClient.orderClose(message);
                    break;
                case DISTRIBUTOR_ORDER_SHIP_PUSH:
                    response = orderCenterFeignClient.ordertShip(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: " + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("OrderCenterListener.orderReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("OrderCenterListener.orderReceive Exception, type=" + type, e);
        }
    }

    @StreamListener(value = OrderCenterBinderConstant.DISTRIBUTOR_REFUND_INPUT)
    public void distributorRefundReceive(DistributorMessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case DISTRIBUTOR_REFUND_REVIEW_PUSH:
                    response = orderCenterFeignClient.refundReview(message);
                    break;
                case DISTRIBUTOR_REFUND_ABANDONED_PUSH:
                    response = orderCenterFeignClient.refundAbandoned(message);
                    break;
                case DISTRIBUTOR_REFUND_CLOSE_PUSH:
                    response = orderCenterFeignClient.refundClose(message);
                    break;
                default:
                    break;
            }
        } catch (RetryableException e) {
            logger.error("OrderCenterListener.distributorRefundReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("OrderCenterListener.distributorRefundReceive Exception, type=" + type, e);
        }
    }
}
